package com.lvyq.myplayer.adapter.collect

import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import androidx.recyclerview.widget.RecyclerView
import com.lvyq.myplayer.R
import com.lvyq.myplayer.model.collect.PlayCollectEntity
import com.lvyq.myplayer.model.history.PlayHistoryEntity
import com.lvyq.myplayer.parcelable.SongInfoData
import com.lvyq.myplayer.widget.common.song.SongItemCheckBoxView

/**
 * 文件描述：PlayHistoryAdapter
 * 作者：lvyq
 * 创建时间：2025-06-18
 * 修改时间：2025-06-18
 */
class PlayCollectOperateAdapter (private val  selectionChangeListener: OnSelectionChangeListener): RecyclerView.Adapter<PlayCollectOperateAdapter.PlayOperateViewHolder>() {

    var listData=ArrayList<PlayCollectEntity>()

    private val selectedPositions = mutableSetOf<Int>()
    private var isSelectAll = false


    // 新增接口用于通知选中状态变化
    interface OnSelectionChangeListener {
        fun onSelectionChanged(selectedCount: Int)
    }





    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlayOperateViewHolder {
            return PlayOperateViewHolder(SongItemCheckBoxView(parent.context))
    }

    override fun getItemCount(): Int {
        return listData.size
    }


    override fun onBindViewHolder(holder: PlayOperateViewHolder, position: Int) {
            val data = listData[position]
            val item = holder.itemView as SongItemCheckBoxView
            item.setData(SongInfoData(data.businessKey, data.bgImg,data.title,data.lrc,
                data.url,data.singer,data.singerId,data.publishDate,data.albumId,data.albumName,null,0,null,null,null,null,null))

            // 设置CheckBox状态
            holder.checkBox.isChecked = selectedPositions.contains(position)

            // 可选：处理长按进入选择模式
            holder.itemView.setOnClickListener {
                toggleSelection(position)
                true
            }

    }

    fun initData(lst: ArrayList<PlayCollectEntity>) {
        listData=lst
        notifyDataSetChanged()
    }

    inner class PlayOperateViewHolder(view:View): RecyclerView.ViewHolder(view) {
        
        val checkBox: CheckBox = view.findViewById(R.id.checkbox_item)
        init {
            checkBox.setOnCheckedChangeListener { _, isChecked ->
                if (isChecked) {
                    selectedPositions.add(adapterPosition)
                } else {
                    selectedPositions.remove(adapterPosition)
                }
                selectionChangeListener?.onSelectionChanged(selectedPositions.size)
            }
        }
    }
    // 切换单个条目选中状态
    fun toggleSelection(position: Int) {
        if (selectedPositions.contains(position)) {
            selectedPositions.remove(position)
        } else {
            selectedPositions.add(position)
        }
        notifyItemChanged(position)
        selectionChangeListener?.onSelectionChanged(selectedPositions.size)
    }

    // 全选
    fun selectAll() {
        selectedPositions.clear()
        selectedPositions.addAll(listData.indices)
        isSelectAll = true
        notifyDataSetChanged()
        selectionChangeListener?.onSelectionChanged(listData.size)
    }

    // 清除所有选中
    fun clearSelections() {
        selectedPositions.clear()
        isSelectAll = false
        notifyDataSetChanged()
        selectionChangeListener?.onSelectionChanged(0)
    }

    // 获取选中的条目
    fun getSelectedItems(): List<PlayCollectEntity> {
        return selectedPositions.map { listData[it] }
    }

}